package com.xxx.controller;

import com.xxx.dto.AlipayNotifyDto;
import com.xxx.dto.PayApplyDto;
import com.xxx.result.JSONResult;
import com.xxx.service.IPayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 检查是否已经生成支付单
     *
     * @param orderNo
     * @return
     */

    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo) {
        return payOrderService.checkPayOrder(orderNo);

    }

    /**
     * 支付请求
     *
     * @return
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto payApplyDto) {
        String payForm = payOrderService.apply(payApplyDto);
        return JSONResult.success(payForm);
    }

    /**
     * 异步回调通知
     * http://58dabm.natappfree.cc/ymcc/pay/pay/alipay/notify
     */
    @PostMapping("/asyncNotify")
    public String asyncNotify(AlipayNotifyDto alipayNotifyDto) {
        try {
            return payOrderService.asyncNotify(alipayNotifyDto);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }
}
